<template>
	<view class="container">
		<view class="message-area">
			<view v-for="message in messages" :key="message.id"
				:class="{'message-item-left': message.type === 'received', 'message-item-right': message.type === 'sent'}">
				<view class="message-avatar">
					<image :src="message.avatar" class="avatar-image" />
				</view>
				<view class="message-content"
					:class="{'message-left': message.type === 'received', 'message-right': message.type === 'sent'}">
					<view class="message-nickname">{{ message.nickname }}</view>
					<view class="message-text">{{ message.content }}</view>
				</view>
			</view>
		</view>

		<view class="input-area">
			<input class="input-box" v-model="inputText" type="text" placeholder="输入消息" />
			<button class="send-button" @click="sendMessage">发送</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				inputText: '',
				messages: [{
						id: 1,
						avatar: '/static/chatUser.png',
						nickname: '小明',
						content: 'Hello',
						type: 'received'
					},
					{
						id: 2,
						avatar: '/static/missing-face.png',
						nickname: '小红',
						content: 'Hi',
						type: 'sent'
					},
				]
			};
		},
		methods: {
			sendMessage() {
				const message = {
					id: this.messages.length + 1,
					avatar: 'https://img0.baidu.com/it/u=2552791859,3777891909&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1692550800&t=6b001fd0911fd78be2e5ace54e67d2de', // 设置发送者的头像
					nickname: '小红', // 设置发送者的昵称
					content: this.inputText,
					type: 'sent'
				};
				this.messages.push(message);
				this.inputText = '';
			}
		}
	};
</script>

<style>
	page {
		background: #f4f4f4;
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	.message-area {
		flex: 1;
		overflow-y: auto;
		padding: 10px;
	}

	.message-item-left {
		display: flex;
		align-items: flex-start;
		margin: 20px 0 20px 0;
	}

	.message-item-right {
		display: flex;
		align-items: flex-start;
		flex-direction: row-reverse;
		margin: 20px 0 20px 0;
	}

	.message-avatar {
		width: 40px;
		height: 40px;
		margin-right: 6px;
	}

	.avatar-image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.message-content {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.message-nickname {
		font-size: 12px;
		color: #666;
		margin-bottom: 4px;
		padding: 1%;
	}

	.message-text {
		padding: 6px 10px;
		border-radius: 8px;
	}

	.message-left {
		background-color: #ffffff;
	}

	.message-right {
		background-color: #e6e6e6;
		text-align: right;
	}

	.input-area {
		flex-shrink: 0;
		display: flex;
		width: 100%;
		align-items: center;
		padding: 10px;
		background-color: #f9f9f9;
		position: absolute;
		bottom: 0;
	}

	.input-box {
		flex: 1;
		height: 40px;
		padding: 0 10px;
		border: 1px solid #ccc;
		border-radius: 4px;
	}

	.send-button {
		margin-left: 10px;
		padding: 0 10px;
		height: 40px;
		line-height: 40px;
		background-color: #ff0000;
		color: #fff;
		border: none;
		border-radius: 4px;
	}
</style>
